if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready',plusReady,false);
}
// var totalData = ''	
function goSales(x, y) {
	// console.log('setpsgInfo', x, y)	
	var flightId = getData("flightId");
	  var legTh = getData("legTh");
	var seatsInfo = getData('PsgInfo'+flightId+legTh);
	console.log(flightId, legTh)
	var flight_data = seatsInfo.msg[0];
	var datas = flight_data.seats;
	$.each(datas, function(i,seat) {
		if (seat.x == x && seat.y == y) {
			var psginfo = seat.personinfo
			var ticketNum = seat.ticketNum
			console.log(seat.seatName)			
			setData('psgInfo', seat)	
			setData('ticketNum', ticketNum)
			setTimeout(()=> {
				uni.navigateTo({
					url: '/pages/index/main?target=sales'
				})		
			}, 100)
		}
	})	
}
function btnAction(psgRemark,o_psg_sn) { 
		
		var flightId = getData("flightId");
	    var legTh = getData("legTh");
		var seatsInfo = getData('PsgInfo'+flightId+legTh);
		var flight_data = seatsInfo.msg[0];
		var datas = flight_data.seats;

		if (psgRemark=="" || psgRemark=="鄂") {
//			window.location.href = "epidemic_new.html";
		} else {
				setData("o_psg_sn",o_psg_sn);
			setData("flightno",flight_data.flightNo);
			window.location.href = "epidemic_detail.html";
			}
	}
function plusReady () {
	

	var flightId = getData("flightId");
	var legTh = getData("legTh");
	//特殊人群数量
//	var oldmanNum = 0;
	
	$(document).ready(function() {		
		
		document.addEventListener('UniAppJSBridgeReady', function() {
			console.log('uReady')
			
		UpdateState();
		load_seatInfo ();
		
		$("#meal").click(function() {
			if ($(this).attr("checked")) {
				$("span.red").show();
			} else {
				$("span.red").hide();
			}
		});
		$("#health").click(function() {
			if ($(this).attr("checked")) {
				$("span.health").show();
			} else {
				$("span.health").hide();
			}
		});
		$("#birth").click(function() {
			if ($(this).attr("checked")) {
				$("span.birthday").addClass("cake");
			} else {
				$("span.birthday").removeClass("cake");
			}
		});
		$("#member").click(function() {
			var member = $("button.isMember");
			if ($(this).attr("checked")) {
				member.css("background","url(images/member.png) no-repeat");
				
			} else {
				member.css("background","url(images/person.png) no-repeat");
			}
		});
		$("#leave").on("touchend",function () {
			// uni.setStorageSync('pageTarget', '')			
			setData("noJump", 1);
			setTimeout(()=> {
				uni.navigateTo({
					url: '/pages/index/main'
				})		
			}, 100)
			// $("#flightpage").html();
		});
	});
	
	function load_seatInfo () {
		UpdateState();
		if (!isOnLine) {
			loadSeats();
		} else {
			var url = server_url+'/9airweb/SeatMap.action?flightid='+flightId+'&legth='+legTh;
//var url = 'http://10.200.107.102:8080/9airweb/SeatMap.action?flightid='+flightId+'&legth='+legTh
			url += url_tail;
			var myDate = new Date();
			console.log(url);
			
			$.ajax({
					dataType: "json",
				    url: url,
				    timeout:30000
			}).done(function(backdata, status){
				if (status=='success') {
					if (setData('PsgInfo'+flightId+legTh, backdata)) {
						var myDate = new Date();
						console.log("数据更新成功！");
						loadSeats();
					}
				}
			}).fail(function(jqXHR, textStatus){
			    if(textStatus == 'timeout') {
			        plus.nativeUI.alert("数据更新失败，即将加载本地数据！","OK");
			        loadSeats();
			    }
			});
		}
	}
	
	function loadSeats () {
		$(".header").show();
		$("#caseVerteClaire").hide();
		var seatsInfo = getData('PsgInfo'+flightId+legTh);
		if (!seatsInfo) {
			plus.nativeUI.alert("本地无数据，请连接网络刷新数据！")
			window.location.href = "index.html";
			return;
		}
		var flight_data = seatsInfo.msg[0];
		var isInternational = flight_data.o_flt_type == "I" ? true : false;
		var datas = flight_data.seats;
		totalData = datas
		
		if (seatsInfo.type != "success") {
			plus.nativeUI.alert(seatsInfo.msg);
		} else{
			//flightNo
			$("h4#flightNo").append(flight_data.flightNo);
			
			//people
			var peopleInfo = "当前舱单人数："+ (flight_data.ad+flight_data.chd+flight_data.inf) +" (成人："+ flight_data.ad +" / 儿童："+ flight_data.chd +" / 婴儿："+ flight_data.inf +")";
			$("#people").append(peopleInfo);
			
			//mealTotal
			var mealtotal = "总计：";
			$.each(flight_data.mealTotal, function(key,value) {
				mealtotal += key + "：" + value + " / ";
			});
			if (mealtotal[mealtotal.length-1] == ' ') {
				mealtotal = mealtotal.substring(0,mealtotal.length-2);
			} else{
				mealtotal = "";
			}
			$("#mealTotal").append(mealtotal);
			
			//flightInfo
			var div_flight = '<div class="row">'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">起飞：'+ flight_data.origtime +'</h5>'
								+'</div>'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">'+ flight_data.orgi +'</h5>'
								+'</div>	'				
							+'</div>'
							+'<div class="row">'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">落地：'+ flight_data.destime +'</h5>'
								+'</div>'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<h5 class="flight">'+ flight_data.dest +'</h5>'
								+'</div>'
							+'</div>';
			$($.parseHTML(div_flight)).appendTo($("#flightInfo"));
			
			setSeats ();
		}
		
		
		//seats
		function setSeats () {
			$("#seats").empty();
			for (var i = 33 ; i >= 1 ;i--) {
				var div_col_1 = '<div class="row" style="text-align:center;">'
									+'<div class="row col-xs-5 col-sm-5 col-md-5">'
										+'<div class="col-xs-1 col-sm-1 col-md-1 number-line" id="'+i+'">'
											+'<div class="number">	'
												+'<span>'+ i +'</span>'
											+'</div>'
										+'</div>'
										+'<div class="row col-xs-11 col-sm-11 col-md-11">';
				var seat_left='';
				for (var j = 5; j >= 3; j--) {
					seat_left += getPsgInfoByXY(j,i);
				}
				var div_col_2 = '</div>'
							+'</div>'
							+'<div class="row col-xs-2 col-sm-2 col-md-2">'
								+'<div class="col-xs-3 col-sm-3 col-md-3"></div>'
								+'<div class="col-xs-6 col-sm-6 col-md-6">'
									+'<div class="aisle"></div>'
								+'</div>'		
								+'<div class="col-xs-3 col-sm-3 col-md-3"></div>'					
							+'</div>'
							+'<div class="row col-xs-5 col-sm-5 col-md-5">'
								+'<div class="row col-xs-11 col-sm-11 col-md-11">';
				var seat_right='';
				for (var j = 2; j >= 0; j--) {
					seat_right += getPsgInfoByXY(j,i);
				}
				var div_col_3 = '</div>'
								+'<div class="col-xs-1 col-sm-1 col-md-1 number-line" id="_'+i+'">'
									+'<div class="number">	'
										+'<span>'+ i +'</span>'
									+'</div>'
								+'</div>'
							+'</div>'
						+'</div>';
				
				var div_col = div_col_1 + seat_left + div_col_2 + seat_right + div_col_3;
				$($.parseHTML(div_col)).appendTo($("#seats"));
			}
			
			//应急出口
			$("#15").removeClass("number-line").addClass("door").html('<div class="safe-door"><span>应</span><br/><span>急</span><br/><span>出</span><br/><span>口</span></div>');
			$("#16").removeClass("number-line").addClass("door").html('<div class="safe-door"><span>应</span><br/><span>急</span><br/><span>出</span><br/><span>口</span></div>');
			$("#_15").removeClass("number-line").addClass("door").html('<div class="safe-door"><span>应</span><br/><span>急</span><br/><span>出</span><br/><span>口</span></div>');
			$("#_16").removeClass("number-line").addClass("door").html('<div class="safe-door"><span>应</span><br/><span>急</span><br/><span>出</span><br/><span>口</span></div>');
			
			//安全门
			$("#1").removeClass("number-line").addClass("door").html('<div class="first-door "><span>舱</span><br/><span>门</span></div>');
			$("#32").removeClass("number-line").addClass("door").html('<div class="first-door "><span>舱</span><br/><span>门</span></div>');
			$("#_1").removeClass("number-line").addClass("door").html('<div class="first-door "><span>舱</span><br/><span>门</span></div>');
			$("#_32").removeClass("number-line").addClass("door").html('<div class="first-door "><span>舱</span><br/><span>门</span></div>');
			
//			if (psgRemark=="") {
//					$("#btn_submit").display = 'none';
//		} else {
//				$("#btn_submit").display = '';
//		}
		
			//添加特殊人群餐总数量
//			if (oldmanNum > 0) {
//				var people = $("#people").html();
//				var newPeopleInfo = people.substring(0,people.length-1) + ' / 特殊人群：'+oldmanNum+')';
//				$("#people").html(newPeopleInfo);
//			}
		}
		
		function getPsgInfoByXY(x, y) {
			var div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'							
								+'<button class="seatn" data-toggle="modal" data-target="#">'
									+'<span>'
									+'</span>'
								+'</button>'
							+'</div>';
			// 先将1排DEF座位设置为空 //TODO
			//if (y==1 && (x==5||x==4||x==3)) {
				//div_seat =""; 
			//}
			$.each(datas, function(i,seat) {
				if (seat.x == x && seat.y == y) {
					var psginfo = seat.personinfo;
					//特殊人群
//					var isOldMan = false;
//					if (parseInt(seat.age) >= 70 || parseInt(seat.age) <= 12) {
//						isOldMan = true;
//						oldmanNum++;
//					}
// 是否是高端经济舱
                     if ((seat.proType == "高端经济舱" || seat.proType.toLowerCase() == "plus") && psginfo.inf == true ) { // 高端婴儿
	             div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'
								+'<button class="seatHInf '+ (psginfo.birth?"isMember":"") +'" onclick="goSales(\'' + x + '\', \''+ y + '\')" data-target="#'+ seat.seatName +'">'
									+'<span class="'+ (psginfo.member?"birthday":"") +'">'
										+'<span class="name">'+ (psginfo.psgName.length>6?(psginfo.psgName.substring(0,6)+"..."):psginfo.psgName) +'</span>'
										+'<br/>'
										+'<span class="tag">['+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他")))  +']</span>'
										+'<br/>'
										+'<span class="white">高端经济舱</span>'
										+'<br/>'
										+'<span class="health" style="color: red;">'+ (seat.psgRemark==""? "未申报": (seat.psgRemark=="鄂" ? "鄂|未申报" : seat.psgRemark)) +'</span>'
									+'</span>'
								+'</button>'
                     }else if((seat.proType == "高端经济舱" || seat.proType.toLowerCase() == "plus") && psginfo.inf == false ){  // 高端不带婴儿
	                       div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'
								+'<button class="seatH '+ (psginfo.birth?"isMember":"") +'" onclick="goSales(\'' + x + '\', \''+ y + '\')" data-target="#'+ seat.seatName +'">'
									+'<span class="'+ (psginfo.member?"birthday":"") +'">'
										+'<span class="name">'+ (psginfo.psgName.length>6?(psginfo.psgName.substring(0,6)+"..."):psginfo.psgName) +'</span>'
										+'<br/>'
										+'<span class="tag">['+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他")))  +']</span>'
										+'<br/>'
										+'<span class="white">高端经济舱</span>'
										+'<br/>'
										+'<span class="health" style="color: red;">'+ (seat.psgRemark==""? "未申报": (seat.psgRemark=="鄂" ? "鄂|未申报" : seat.psgRemark)) +'</span>'
									+'</span>'
								+'</button>'
                     }else if (!(seat.proType == "高端经济舱" || seat.proType.toLowerCase() == "plus") && psginfo.inf == true){ //  普通不带婴儿
	                      div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'
								+'<button class="seatInf '+ (psginfo.birth?"isMember":"") +'" onclick="goSales(\'' + x + '\', \''+ y + '\')" data-target="#'+ seat.seatName +'">'
									+'<span class="'+ (psginfo.member?"birthday":"") +'">'
										+'<span class="name">'+ (psginfo.psgName.length>6?(psginfo.psgName.substring(0,6)+"..."):psginfo.psgName) +'</span>'
										+'<br/>'
										+'<span class="tag">['+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他")))  +']</span>'
										+'<br/>'
										+'<span class="red">'+ psginfo.meal +'</span>'
										+'<br/>'
										+'<span class="health" style="color: red;">'+ (seat.psgRemark==""? "未申报": (seat.psgRemark=="鄂" ? "鄂|未申报" : seat.psgRemark)) +'</span>'
									+'</span>'
								+'</button>'
                     }else {
                     	div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'
								+'<button class="seat '+ (psginfo.birth?"isMember":"") +'" onclick="goSales(\'' + x + '\', \''+ y + '\')" data-target="#'+ seat.seatName +'">'
									+'<span class="'+ (psginfo.member?"birthday":"") +'">'
										+'<span class="name">'+ (psginfo.psgName.length>6?(psginfo.psgName.substring(0,6)+"..."):psginfo.psgName) +'</span>'
										+'<br/>'
										+'<span class="tag">['+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他")))  +']</span>'
										+'<br/>'
										+'<span class="red">'+ psginfo.meal +'</span>'
										+'<br/>'
										+'<span class="health" style="color: red;">'+ (seat.psgRemark==""? "未申报": (seat.psgRemark=="鄂" ? "鄂|未申报" : seat.psgRemark)) +'</span>'
									+'</span>'
								+'</button>'
                     }

// 修改
//					div_seat = '<div class="col-xs-4 col-sm-4 col-md-4">'
//								+'<button class="seat '+ (psginfo.birth?"isMember":"") +'" onclick="goSales(\'' + x + '\', \''+ y + '\')" data-target="#'+ seat.seatName +'">'
//									+'<span class="'+ (psginfo.member?"birthday":"") +'">'
//										+'<span class="name">'+ (psginfo.psgName.length>6?(psginfo.psgName.substring(0,6)+"..."):psginfo.psgName) +'</span>'
//										+'<br/>'
//										+'<span class="tag">['+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他")))  +']</span>'
//										+'<br/>'
//										+'<span class="red">'+ psginfo.meal +'</span>'
//									+'</span>'
//								+'</button>'
								
							  div_seat += '<div class="modal fade" id="'+ seat.seatName +'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'
										+'<div class="modal-dialog">'
											+'<div class="modal-content">'
												+'<div class="modal-header">'
													+'<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>'
													+'<h5 class="modal-title" id="">乘客信息</h5>'
												+'</div>'
												+'<div class="modal-body" style="padding-bottom: 0px">'
													+'<table class="table" style="text-align: left">'
														+'<thead>'
															+'<tr>'
																+'<th>#</th>'
																+'<th>名称</th>'
																+'<th>内容</th>'
															+'</tr>'
														+'</thead>'
														+'<tbody>'
															+'<tr>'
																+'<td>1</td>'
																+'<td>座位号</td>'
																+'<td>'+ seat.seatName +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>2</td>'
																+'<td>旅客姓名</td>'
																+'<td>'+ psginfo.psgName +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>3</td>'
																+'<td>性别</td>'
																+'<td>'+ (psginfo.psgType == "ADT"?(psginfo.psgGender=="F"?(psginfo.inf==true?"女/带婴儿":"女"):(psginfo.psgGender=="M"?(psginfo.inf==true?"男/带婴儿":"男"):(psginfo.psgGender=="CHD"?"儿童":(psginfo.psgGender == "INF" ? "婴儿":"其他")))):(psginfo.psgType == "CHD"?"儿童":(psginfo.psgType == "INF"?"婴儿":"其他"))) +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>4</td>'
																+'<td>带婴儿</td>'
																+'<td>'+ (psginfo.inf==true?"是":"否") +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>5</td>'
																+'<td>订餐类型</td>'
																+'<td>'+ (psginfo.meal==""?"无":psginfo.meal) +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>6</td>'
																+'<td>目的地</td>'
																+'<td>'+ psginfo.dest +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>7</td>'
																+'<td>生日</td>'
																+'<td>'+ (psginfo.birth==true?"是":"否") +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>8</td>'
																+'<td>会员</td>'
																+'<td>'+ (psginfo.member==true?"是":"否") +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>9</td>'
																+'<td>手机号</td>'
																+'<td>'+ (psginfo.tel==""?"无":psginfo.tel) +'</td>'
															+'</tr>'
															+'<tr>'
																+'<td>10</td>'
																+'<td>健康申报</td>'
																+'<td>'+ (seat.psgRemark==""? "未申报": (seat.psgRemark=="鄂" ? "鄂|未申报" : seat.psgRemark)) +'</td>'
															+'</tr>'
														+'</tbody>'
													+'</table>'
												+'</div>'
												+'<div class="modal-footer" style="padding-top: 0px">'
												if(seat.psgRemark == "" || seat.psgRemark=="鄂") {
													
												} else {
													div_seat +='<button type="button" id="btn_submit" onclick="btnAction(\''+ seat.psgRemark +'\',\''+ seat.o_psg_sn +'\')" class="btn btn-primary" data-dismiss="modal-health" style="margin-right: 50px;border: none;">申报详情</button>'
												}
													div_seat +='<button type="button" style="margin-right: 40px" class="btn btn-info" onclick="goSales(\'' + x + '\', \''+ y + '\')">机上销售</button>'
													
													div_seat +='<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>'
												+'</div>'
											+'</div>'
										+'</div>'
									+'</div>'
							+'</div>';
							
				}
			});
			
			return div_seat;
		}
	}
	})
}